package com.taotao.controller;

import com.taotao.pojo.TaotaoResult;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.service.ItemService;
import com.taotao.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/item")
public class ItemController {

	@Autowired
	private ItemService itemService;
	
	@RequestMapping("/{itemId}")
	@ResponseBody
	public TbItem getItemById(@PathVariable(value="itemId") Long itemId) {
		TbItem item = itemService.getItemById(itemId);
		return item;
	}

    @RequestMapping("/html/{itemId}")
    @ResponseBody
    public ModelAndView getItemParamHtml(@PathVariable(value="itemId") Long itemId) {
        ModelAndView mav = new ModelAndView("itemParam");
        String html = itemService.getItemParamHtml(itemId);
        mav.addObject("htmltable",html);
        return mav;
    }

    @RequestMapping("/save")
    @ResponseBody
    public TaotaoResult creditItem(TbItem item, String desc,String itemParams) {
        TaotaoResult result = itemService.createItem(item,desc,itemParams);
        return result;
    }

    @RequestMapping("/update")
    @ResponseBody
    public TaotaoResult updateItem(TbItem item, String desc,String itemParams,Long itemParamId) {
        TaotaoResult result = itemService.updateItem(item,desc,itemParams,itemParamId);
        return result;
    }

    /**
     * 商品描述信息回显
     * @param itemid
     * @return
     */
    @RequestMapping("/desc/{itemid}")
    @ResponseBody
    public TbItemDesc getItemByItemid(@PathVariable Long itemid) {
        TbItemDesc desc = itemService.getItemByItemid(itemid);
        return desc;
    }

    /**
     * 商品规格参数回显
     * @param itemid
     * @return
     */
    @RequestMapping("/param/item/{itemid}")
    @ResponseBody
    public TaotaoResult getItemCatByItemid(@PathVariable Long itemid) {
        TaotaoResult result = itemService.getItemCatByItemid(itemid);
        return result;
    }
    //查询商品返回结果
    @RequestMapping("/list")
    @ResponseBody
    public String getItemList(Integer page, Integer rows) {
        return JsonUtils.objectToJson(itemService.getItemList(page,rows));
    }
}
